package com.teamtgv.shareshark;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.teamtgv.shareshark.R;
import com.teamtgv.shareshark.result.Result;

/**
 * 
 * This class expects data passed via
 * myIntent.putExtra(ResultViewActivity.RESULT_OBJECT, resultItem);
 * 
 */
public class ResultViewActivity extends Activity {

	public static final String LOG_TAG = ShareActivity.class.getSimpleName();
	public static final String RESULT_OBJECT = "result_object";
	
	public static final String RECOMMENDED_BY_PREFIX = "By: ";
	public static final String LOCATION_PREFIX = "Location: ";
	
	private Result result;
	private boolean upvoted = false;
	private boolean downvoted = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_result_view);

		// Get result object from intent data
		result = (Result) getIntent().getParcelableExtra(RESULT_OBJECT);
		if (result != null) {
			// Show the data from the Result in the layout
			
			
			
			String itemWhat = result.getSubject();
			String itemBy = RECOMMENDED_BY_PREFIX + result.getRecommender();
			String itemWhere = LOCATION_PREFIX + result.getLocation();
			String itemDate = result.getDateRecommended();
			
			((TextView) ResultViewActivity.this.findViewById(R.id.textResultWhat)).setText(itemWhat);
			((TextView) ResultViewActivity.this.findViewById(R.id.textResultLocation)).setText(itemWhere);
			((TextView) ResultViewActivity.this.findViewById(R.id.textResultRecommender)).setText(itemBy);
			((TextView) ResultViewActivity.this.findViewById(R.id.textResultTime)).setText(itemDate);
			
			if (result.getRecommender().equals(UserData.UNKNOWN_USER)) {
				((TextView) ResultViewActivity.this.findViewById(R.id.textResultRecommender)).setHeight(0); // hide field
			}
			
			
			final ImageView imageView = ((ImageView) ResultViewActivity.this.findViewById(R.id.imageViewResult));
			if (result.hasPhoto()) {
				Log.v(LOG_TAG, "Result has image at " + result.getPhotoUrl());
				try {
					ImageShack imageShack = new ImageShack();
					final Bitmap image = imageShack.download(this, result.getPhotoUrl());
					imageView.setImageBitmap(image);

					imageView.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							Intent myIntent = new Intent(
									ResultViewActivity.this,
									ImagePopupActivity.class);
							myIntent.putExtra(ImagePopupActivity.IMAGE, image);
							ResultViewActivity.this.startActivity(myIntent);
						}
					});

				} catch (Exception e) {
					Log.v("ResultViewActivity", e.toString() + ": " + e.getMessage());
				}

			} else {
				Log.v(LOG_TAG, "Result has no image");
				imageView.setPadding(0, 0, 0, 0);
				
				imageView.setVisibility(View.INVISIBLE);
			}

		}

		final Button thumbsUpButton = (Button) this
				.findViewById(R.id.buttonThumbsUp);
		thumbsUpButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!upvoted) {
					ResultViewActivity.this.findViewById(R.id.buttonThumbsUp).setBackgroundResource(R.drawable.thumbs_up_pressed);
					result.increaseRating();
					upvoted = true;
				} else {
					ResultViewActivity.this.findViewById(R.id.buttonThumbsUp)
							.setBackgroundResource(R.drawable.thumbs_up);
					result.undoIncreaseRating();
					upvoted = false;
				}
			}
		});
		final Button thumbsDownButton = (Button) this
				.findViewById(R.id.buttonThumbsDown);
		thumbsDownButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!downvoted) {
					ResultViewActivity.this.findViewById(R.id.buttonThumbsDown)
							.setBackgroundResource(
									R.drawable.thumbs_down_pressed);
					result.decreaseRating();
					downvoted = true;
				} else {
					ResultViewActivity.this.findViewById(R.id.buttonThumbsDown)
							.setBackgroundResource(R.drawable.thumbs_down);
					result.undoDecreaseRating();
					downvoted = false;
				}
			}
		});

		if (result.isUpvoted()) {
			upvoted = true;
			ResultViewActivity.this.findViewById(R.id.buttonThumbsUp)
					.setBackgroundResource(R.drawable.thumbs_up_pressed);
		}

		if (result.isDownvoted()) {
			downvoted = true;
			ResultViewActivity.this.findViewById(R.id.buttonThumbsDown)
					.setBackgroundResource(R.drawable.thumbs_down_pressed);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.result_view, menu);
		return false;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
